package com.coolgor.coldot.dao.stock;

import com.coolgor.coldot.entity.stock.StockInfo;
import org.apache.ibatis.annotations.*;
import java.util.List;


@Mapper
public interface StockInfoDao {

	@Select("""
		<script>
			SELECT
				*
			FROM
				tb_coldot_stock_info
			WHERE
				record_id = #{recordID}
		</script>
	""")
	@Results(id = "StockInfoMap", value = {
		@Result(property = "recordID", column = "record_id"),
		@Result(property = "code", column = "code"),
		@Result(property = "name", column = "name"),
		@Result(property = "group", column = "group"),
		@Result(property = "flag", column = "flag"),
		@Result(property = "createTime", column = "create_time"),
		@Result(property = "lastEditTime", column = "last_edit_time"),
		@Result(property = "remark", column = "remark")
	})
	StockInfo queryByID(@Param("recordID") Long recordID);

	@Select("""
		<script>
			SELECT
				*
			FROM
				tb_coldot_stock_info
			<where>
				<if test="stockInfoCondition.recordID != null">
					and record_id = #{stockInfoCondition.recordID}
				</if>
				<if test="stockInfoCondition.code != null">
					and code like '${stockInfoCondition.code}'
				</if>
				<if test="stockInfoCondition.name != null">
					and name like '${stockInfoCondition.name}'
				</if>
				<if test="stockInfoCondition.group != null">
					and group like '${stockInfoCondition.group}'
				</if>
				<if test="stockInfoCondition.flag != null">
					and flag = #{stockInfoCondition.flag}
				</if>
				<if test="stockInfoCondition.createTime != null">
					and create_time = #{stockInfoCondition.createTime}
				</if>
				<if test="stockInfoCondition.createTime != null">
					and <![CDATA[ create_time >= #{stockInfoCondition.createTime,jdbcType=DATE} ]]>
				</if>
				<if test="stockInfoCondition.createTime != null">
					and <![CDATA[ create_time <= #{stockInfoCondition.createTime,jdbcType=DATE} ]]>
				</if>
				<if test="stockInfoCondition.lastEditTime != null">
					and last_edit_time = #{stockInfoCondition.lastEditTime}
				</if>
				<if test="stockInfoCondition.lastEditTime != null">
					and <![CDATA[ last_edit_time >= #{stockInfoCondition.lastEditTime,jdbcType=DATE} ]]>
				</if>
				<if test="stockInfoCondition.lastEditTime != null">
					and <![CDATA[ last_edit_time <= #{stockInfoCondition.lastEditTime,jdbcType=DATE} ]]>
				</if>
				<if test="stockInfoCondition.remark != null">
					and remark like '${stockInfoCondition.remark}'
				</if>
			</where>
			LIMIT #{rowIndex},#{pageSize};
		</script>
	""")
	@ResultMap("StockInfoMap")
	List<StockInfo> queryList(@Param("stockInfoCondition") StockInfo stockInfoCondition,
							@Param("rowIndex") int rowIndex,
							@Param("pageSize") int pageSize);

	@Select("""
		<script>
			SELECT
				count(1)
			FROM
				tb_coldot_stock_info
			<where>
				<if test="stockInfoCondition.recordID != null">
					and record_id = #{stockInfoCondition.recordID}
				</if>
				<if test="stockInfoCondition.code != null">
					and code like '${stockInfoCondition.code}'
				</if>
				<if test="stockInfoCondition.name != null">
					and name like '${stockInfoCondition.name}'
				</if>
				<if test="stockInfoCondition.group != null">
					and group like '${stockInfoCondition.group}'
				</if>
				<if test="stockInfoCondition.flag != null">
					and flag = #{stockInfoCondition.flag}
				</if>
				<if test="stockInfoCondition.createTime != null">
					and create_time = #{stockInfoCondition.createTime}
				</if>
				<if test="stockInfoCondition.createTime != null">
					and <![CDATA[ create_time >= #{stockInfoCondition.createTime,jdbcType=DATE} ]]>
				</if>
				<if test="stockInfoCondition.createTime != null">
					and <![CDATA[ create_time <= #{stockInfoCondition.createTime,jdbcType=DATE} ]]>
				</if>
				<if test="stockInfoCondition.lastEditTime != null">
					and last_edit_time = #{stockInfoCondition.lastEditTime}
				</if>
				<if test="stockInfoCondition.lastEditTime != null">
					and <![CDATA[ last_edit_time >= #{stockInfoCondition.lastEditTime,jdbcType=DATE} ]]>
				</if>
				<if test="stockInfoCondition.lastEditTime != null">
					and <![CDATA[ last_edit_time <= #{stockInfoCondition.lastEditTime,jdbcType=DATE} ]]>
				</if>
				<if test="stockInfoCondition.remark != null">
					and remark like '${stockInfoCondition.remark}'
				</if>
			</where>
		</script>
	""")
	int queryCount(@Param("stockInfoCondition") StockInfo stockInfoCondition);

	@Insert("""
		<script>
			INSERT INTO tb_coldot_stock_info (
				code,
				name,
				group,
				flag,
				create_time,
				last_edit_time,
				remark
			) VALUES (
				#{code},
				#{name},
				#{group},
				#{flag},
				#{createTime},
				#{lastEditTime},
				#{remark}
			)
		</script>
	""")
	@Options(useGeneratedKeys = true, keyColumn = "record_id", keyProperty = "recordID")
	int insert(StockInfo stockInfo);

	@Update("""
		<script>
			UPDATE tb_coldot_stock_info
			<set>
				<if test="code != null">code=#{code},</if>
				<if test="name != null">name=#{name},</if>
				<if test="group != null">group=#{group},</if>
				<if test="flag != null">flag=#{flag},</if>
				<if test="createTime != null">create_time=#{createTime},</if>
				<if test="lastEditTime != null">last_edit_time=#{lastEditTime},</if>
				<if test="remark != null">remark=#{remark}</if>
			</set>
			WHERE
				record_id = #{recordID}
		</script>
	""")
	int update(StockInfo stockInfo);

	@Delete("""
		<script>
			DELETE FROM tb_coldot_stock_info
			WHERE
				record_id = #{recordID}
		</script>
	""")
	int delete(Long recordID);

	@Delete("""
		<script>
			DELETE FROM tb_coldot_stock_info
			WHERE
				record_id IN
				<foreach collection="list" item="recordID" open="(" separator="," close=")">
					#{recordID}
				</foreach>
		</script>
	""")
	int deleteBatch(List<Long> idList);

}